﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core;
using Core.DAL;
using System.Collections;

namespace xManagerWebApp.__Membership
{
    public partial class WF_ExpensesForApproval : System.Web.UI.Page
    {

        private Dictionary<Guid, PlaceHolder> placeholders;

        protected override void OnInit(EventArgs e)
        {
            //base.OnInit(e);

            //placeholders = new Dictionary<Guid, PlaceHolder>();

            //IEnumerable<UserDepartment> uds; 
            //using(DataAccessLayer dc = new DataAccessLayer())
            //{
            //    uds = Core.User.GetDepartmentLevels(dc, Core.User.CurrentUserId.Value);
            

            //foreach (UserDepartment ud in uds)
            //{
            //}

            //}
        }

        //protected override void OnPreRender(EventArgs e)
        //{
        //    base.OnPreRender(e);
        //    if (IsPostBack)
        //    {
        //        placeholders = new Dictionary<Guid, PlaceHolder>();
        //        this.ExpensesLists.Controls.Clear();
        //        using (DataAccessLayer dc = new DataAccessLayer())
        //        {

        //            foreach (UserDepartment u in Core.User.GetDepartmentLevels(dc, "pkunha@gmail.com"))
        //            {
        //                LiteralControl l = new LiteralControl("<h3>Department level " + u.DepartmentLevel.Name + ":</h3>");
        //                PlaceHolder p = new PlaceHolder();
        //                p.ID = u.Id.ToString();


        //                placeholders.Add(u.Id, p);
        //                this.ExpensesLists.Controls.Add(l);
        //                this.ExpensesLists.Controls.Add(p);

        //                foreach (NotificationQueue nq in NotificationQueue.Get(dc, u.Id, false))
        //                    AddExpense(nq, placeholders[u.Id]);
        //            }
        //        }
        //    }

        //}

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            placeholders = new Dictionary<Guid, PlaceHolder>();

            

            using (DataAccessLayer dc = new DataAccessLayer())
            {

                var query = from NQ in dc.DataContext.NotificationQueues
                            join UD in dc.DataContext.UserDepartments on NQ.DepartmentLevelId equals UD.Id
                            where UD.UserId.Equals(Core.User.CurrentUserId) && NQ.Closed.Equals(false)
                            orderby NQ.Created_at 
                            orderby UD.DepartmentLevel.Name                                                       
                            select new
                            {
                                ExpenseLineId = NQ.ExpenseLineId,
                                DepartmentLevelId = NQ.DepartmentLevelId,
                                Description = NQ.ExpenseLine.Description,
                                Employee = NQ.ExpenseLine.ExpenseReport.User.Shortname,
                                DepartmentLevel = UD.DepartmentLevel.Name,
                                Date = NQ.ExpenseLine.Date.Value.ToShortDateString()
                            };

                this.ExpensesForApprovalList.Controls.Clear();
                if (query.Count() != 0)
                {
                    this.ExpensesForApprovalList.DataSource = query;
                    this.ExpensesForApprovalList.DataBind();
                }
                else
                {
                    this.no_expenses.Visible = true;
                }                
            }
        }

        private void AddExpense(NotificationQueue nq, PlaceHolder p)
        {                       
            UC_ExpenseApprovalControl control = (UC_ExpenseApprovalControl) LoadControl("UC_ExpenseApprovalControl.ascx");
            control.ExpenseLineID = nq.ExpenseLineId;
            control.DepartmentLevelID = nq.DepartmentLevelId;
            p.Controls.Add(control);
            
        }

        protected void process(object sender, EventArgs e)
        {
            Button b = (Button)sender;
            String s = b.CommandArgument;

            this.ctrExpenseLine.HIDEBUTTONS = false;

            String[] x = s.Split(':');

            this.ctrExpenseLine.ExpenseLineID = new Guid(x[0]);
            this.ctrExpenseLine.DepartmentLevelID = new Guid(x[1]);
            this.ctrExpenseLine.Visible = true;

        }
    }
}
